
#include "login.h"
#include "login_p.h"
#include "gradientbutton.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSvgRenderer>
#include <QPainter>
#include <QAction>

LoginPrivate::LoginPrivate(Login *q)
    : q_ptr(q)
    , is_hide_passwd_(true)
{
    initUi();
}

LoginPrivate::~LoginPrivate()
{

}

void LoginPrivate::initUi()
{
    QWidget *widget = new QWidget(q_ptr);
    widget->setObjectName("login_widget");
    QHBoxLayout *layout = new QHBoxLayout(q_ptr);
    layout->setMargin(0);
    layout->addWidget(widget);

    auto *gLayout = new QGridLayout(widget);
    // 加载 SVG 文件
    QSvgRenderer svgRenderer(QStringLiteral(":/resources/svg/username.svg"));
    // 创建一个 QPixmap，大小与 SVG 默认大小匹配
    QPixmap pixmap(25, 25);
    pixmap.fill(Qt::transparent);  // 设置背景透明
    // 使用 QPainter 将 SVG 渲染到 QPixmap
    QPainter painter(&pixmap);
    line_username_ = new QLineEdit(widget);
    line_username_->setObjectName("line_login");
    line_username_->setPlaceholderText(QObject::tr("用户名"));
    pixmap.fill(Qt::transparent);  // 设置背景透明
    svgRenderer.render(&painter);
    QAction *leftIconAction = new QAction( line_username_);
    leftIconAction->setIcon(QIcon(pixmap));
    line_username_->addAction(leftIconAction, QLineEdit::LeadingPosition);
    gLayout->addWidget(line_username_, 0, 0, 1, 2);
    line_passwd_ = new QLineEdit(widget);
    line_passwd_->setObjectName("line_login");
    line_passwd_->setEchoMode(QLineEdit::Password);
    line_passwd_->setPlaceholderText(QObject::tr("密码"));
    QAction *leftIconAction2 = new QAction( line_passwd_);
    svgRenderer.load(QStringLiteral(":/resources/svg/passwd.svg"));
    pixmap.fill(Qt::transparent);  // 设置背景透明
    svgRenderer.render(&painter);
    leftIconAction2->setIcon(QIcon(pixmap));
    QAction *rightIconAction = new QAction( line_passwd_);
    pixmap.fill(Qt::transparent);  // 设置背景透明
    svgRenderer.load(QStringLiteral(":/resources/svg/show.svg"));
    svgRenderer.render(&painter);
    rightIconAction->setIcon(QIcon(pixmap));
    line_passwd_->addAction(leftIconAction2, QLineEdit::LeadingPosition);
    line_passwd_->addAction(rightIconAction, QLineEdit::TrailingPosition);
    gLayout->addWidget(line_passwd_, 1, 0, 1, 2);

    auto *btnLogin = new GradientButton(QObject::tr("登录"), widget);
    auto *btnRegister = new GradientButton(QObject::tr("注册"), widget);
    gLayout->addWidget(btnLogin, 2, 0);
    gLayout->addWidget(btnRegister, 2, 1);

    Login::setTabOrder(line_username_, line_passwd_);
    Login::setTabOrder(line_passwd_, btnLogin);
    Login::setTabOrder(btnLogin, btnRegister);
    line_username_->setFocus();

    QObject::connect(btnRegister, &QAbstractButton::clicked, q_ptr, &Login::btnRegisterClicked);
    QObject::connect(btnLogin, &QAbstractButton::clicked, q_ptr, &Login::btnLoginClicked);
    QObject::connect(rightIconAction, &QAction::triggered, q_ptr, &Login::showHideActionTriggered);
}

Login::Login(QWidget *parent)
    : QDialog(parent)
    , d_ptr(new LoginPrivate(this))
{

}

Login::~Login()
{
    if (d_ptr)
    {
        delete d_ptr;
        d_ptr = nullptr;
    }
}

void Login::btnRegisterClicked()
{
    emit sig_register();
}

void Login::btnLoginClicked()
{
    emit sig_login();
}

void Login::showHideActionTriggered()
{
    QSvgRenderer svgRenderer;
    if (d_ptr->is_hide_passwd_)
    {
        svgRenderer.load(QStringLiteral(":/resources/svg/hide.svg"));
        d_ptr->line_passwd_->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        svgRenderer.load(QStringLiteral(":/resources/svg/show.svg"));
        d_ptr->line_passwd_->setEchoMode(QLineEdit::Password);
    }
    auto *action = dynamic_cast<QAction*>(sender());
    QPixmap pixmap(25, 25);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);                      // 设置背景透明
    svgRenderer.render(&painter);
    action->setIcon(QIcon(pixmap));
    d_ptr->is_hide_passwd_ = !d_ptr->is_hide_passwd_;
}
